.globl argmax

.text
# =================================================================
# FUNCTION: Given a int array, return the index of the largest
#   element. If there are multiple, return the one
#   with the smallest index.
# Arguments:
#   a0 (int*) is the pointer to the start of the array
#   a1 (int)  is the # of elements in the array
# Returns:
#   a0 (int)  is the first index of the largest element
# Exceptions:
#   - If the length of the array is less than 1,
#     this function terminates the program with error code 36
# =================================================================
argmax:
	# Prologue
    add t1, a0, x0 # address
    add t2, x0, x0 # index
    lw t3, 0(a0)   # max
    add a0, t2, x0 # return
    bgt a1, x0, loop_start
    li a0, 36
    j exit
loop_start:
    addi t1, t1, 4 # t1 = t1 + 4
    addi t2, t2, 1 # t2 = t2 + 1
    beq t2, a1, loop_end
    lw t4, 0(t1)
    bgt t3, t4, loop_continue
    add t3, t4, x0
    add a0, t2, x0
loop_continue:
    j loop_start
loop_end:
	# Epilogue
	ret